package com.znyj.cloud.device.pack;

import com.alibaba.fastjson.JSONObject;
import com.znyj.cloud.device.constant.SysConstant;
import com.znyj.cloud.device.entity.ReportEntity;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import java.sql.Timestamp;

/**
 * Created by gaoliwei02 on 17/5/29.
 */
public class ReportReceivePackage extends BasePackage {

    private static final Logger logger = Logger.getLogger(ReportReceivePackage.class);

    public ReportReceivePackage() {
        reportEntity = new ReportEntity();
    }

    public ReportEntity getReportEntity() {
        return reportEntity;
    }

    public void setReportEntity(ReportEntity reportEntity) {
        this.reportEntity = reportEntity;
    }

    private ReportEntity reportEntity;

    public void parseJsonData(JSONObject json) {
        String devid = json.getString(SysConstant.REPORT_KEY.DEVICE_ID);

        //老版本1.4.0没有DEVID字段，IMEI->DEVID IMEI->OLD
        /*
        {
                "IMEI": "862991419823825",
                "ID": "112173300002",

                "CSQ": 23,
                "LOC": "E,,N,",
                "BCC": "",
                "ERR": "",
                "IGF": false,
                "BL": 100,
                "BS": "open",
                "BT": "",
                "MVER": "Luat_V0004_Air202_DEBUG",
                "BV": "",
                "PTY": "hb_pac",
                "SN": "0000000000"
        }
*/
        if (devid == null) {
            logger.info("get a old device");
            reportEntity.setDeviceId(json.getString(SysConstant.REPORT_KEY.IMEI));
            reportEntity.setImei("OLD");

            reportEntity.setSigQuality(json.getInteger(SysConstant.REPORT_KEY.SIG_QUALITY));
            reportEntity.setLocation(json.getString(SysConstant.REPORT_KEY.LOCATION));
            reportEntity.setBatteryCycleTime(json.getString(SysConstant.REPORT_KEY.BATTERY_CYCLETIME));
            reportEntity.setErrorInfo(json.getString(SysConstant.REPORT_KEY.ERROR_STATUS));
            reportEntity.setIsGpsFix(json.getBoolean(SysConstant.REPORT_KEY.IS_GPSFIX));
            reportEntity.setBatteryLevel(json.getInteger(SysConstant.REPORT_KEY.BATTREY_LEVEL));
            reportEntity.setBatteryStatus(json.getString(SysConstant.REPORT_KEY.BATTERY_STATUS));
            reportEntity.setBatteryTemperature(json.getString(SysConstant.REPORT_KEY.BATTERY_TEMPERATURE));
            reportEntity.setmVersion(json.getString(SysConstant.REPORT_KEY.M_VERSION));
            reportEntity.setBatteryVoltage(json.getString(SysConstant.REPORT_KEY.BATTERY_VOLTAGE));

            reportEntity.setReportTime(new Timestamp(System.currentTimeMillis()));
            reportEntity.setMac("unsupport");

            reportEntity.setfVersion("1.4.0 below");
            reportEntity.setChargeIn("unsupport");
            reportEntity.setStatusA(-1);
            reportEntity.setStatusB(-1);
        }
        //初始化，工厂设备111170199999
        else if (devid.equals("111170199999")) {
            logger.info("get a fac device");
            reportEntity.setDeviceId(json.getString(SysConstant.REPORT_KEY.IMEI));
            reportEntity.setImei("FAC");

            reportEntity.setSigQuality(json.getInteger(SysConstant.REPORT_KEY.SIG_QUALITY));
            reportEntity.setLocation(json.getString(SysConstant.REPORT_KEY.LOCATION));
            reportEntity.setBatteryCycleTime(json.getString(SysConstant.REPORT_KEY.BATTERY_CYCLETIME));
            reportEntity.setErrorInfo(json.getString(SysConstant.REPORT_KEY.ERROR_STATUS));
            reportEntity.setIsGpsFix(json.getBoolean(SysConstant.REPORT_KEY.IS_GPSFIX));
            reportEntity.setBatteryLevel(json.getInteger(SysConstant.REPORT_KEY.BATTREY_LEVEL));
            reportEntity.setBatteryStatus(json.getString(SysConstant.REPORT_KEY.BATTERY_STATUS));
            reportEntity.setBatteryTemperature(json.getString(SysConstant.REPORT_KEY.BATTERY_TEMPERATURE));
            reportEntity.setmVersion(json.getString(SysConstant.REPORT_KEY.M_VERSION));
            reportEntity.setBatteryVoltage(json.getString(SysConstant.REPORT_KEY.BATTERY_VOLTAGE));

            reportEntity.setReportTime(new Timestamp(System.currentTimeMillis()));
            reportEntity.setMac(json.getString(SysConstant.REPORT_KEY.MAC));

            reportEntity.setfVersion(json.getString(SysConstant.REPORT_KEY.F_VERSION));
            reportEntity.setChargeIn(json.getString(SysConstant.REPORT_KEY.CHARGE_IN));
            reportEntity.setStatusA(json.getInteger(SysConstant.REPORT_KEY.BATTERY_STATUS_A));
            reportEntity.setStatusB(json.getInteger(SysConstant.REPORT_KEY.BATTERY_STATUS_B));
        }
        //正常V1.5.0以上
        else {
            /*
        {
               "IMEI": "868575020843266",
               "ID": "112173300002",

               "CSQ": 21,
               "LOC": "E,,N,",
               "BCC": "",
               "ERR": "",
               "IGF": false,
               "BL": 1,
               "BS": "close",
               "BT": "",
               "MVER": "Luat_V0005_Air202",
               "BV": "",
               "PTY": "hb_pac",

               "BSB": 0,
               "BSA": 0,
               "CS": "off",
               "FVER": "1.5.0"
        }
*/
            reportEntity.setDeviceId(devid);
            reportEntity.setImei(json.getString(SysConstant.REPORT_KEY.IMEI));

            reportEntity.setSigQuality(json.getInteger(SysConstant.REPORT_KEY.SIG_QUALITY));
            reportEntity.setLocation(json.getString(SysConstant.REPORT_KEY.LOCATION));
            reportEntity.setBatteryCycleTime(json.getString(SysConstant.REPORT_KEY.BATTERY_CYCLETIME));
            reportEntity.setErrorInfo(json.getString(SysConstant.REPORT_KEY.ERROR_STATUS));
            reportEntity.setIsGpsFix(json.getBoolean(SysConstant.REPORT_KEY.IS_GPSFIX));
            reportEntity.setBatteryLevel(json.getInteger(SysConstant.REPORT_KEY.BATTREY_LEVEL));
            reportEntity.setBatteryStatus(json.getString(SysConstant.REPORT_KEY.BATTERY_STATUS));
            reportEntity.setBatteryTemperature(json.getString(SysConstant.REPORT_KEY.BATTERY_TEMPERATURE));
            reportEntity.setmVersion(json.getString(SysConstant.REPORT_KEY.M_VERSION));
            reportEntity.setBatteryVoltage(json.getString(SysConstant.REPORT_KEY.BATTERY_VOLTAGE));

            reportEntity.setReportTime(new Timestamp(System.currentTimeMillis()));
            reportEntity.setMac(json.getString(SysConstant.REPORT_KEY.MAC));


            reportEntity.setfVersion(json.getString(SysConstant.REPORT_KEY.F_VERSION));
            reportEntity.setChargeIn(json.getString(SysConstant.REPORT_KEY.CHARGE_IN));
            reportEntity.setStatusA(json.getInteger(SysConstant.REPORT_KEY.BATTERY_STATUS_A));
            reportEntity.setStatusB(json.getInteger(SysConstant.REPORT_KEY.BATTERY_STATUS_B));


        }

    }

    public void updateReportEntity(ReportEntity reportEntity, String misc) {
        BeanUtils.copyProperties(this.reportEntity, reportEntity);
        reportEntity.setOnlineStatus("online");
        reportEntity.setMiseMessage(misc);
    }

    public void insertFirstEntity(ReportEntity reportEntity) {
        BeanUtils.copyProperties(this.reportEntity, reportEntity);
        reportEntity.setOnlineStatus("online");
        reportEntity.setRegisterTime(new Timestamp(System.currentTimeMillis()));
        reportEntity.setMiseMessage("");

        reportEntity.setOrderCreateTimeA(new Timestamp(SysConstant.DEFAULT_TIME));
        reportEntity.setOrderCreateTimeB(new Timestamp(SysConstant.DEFAULT_TIME));
        reportEntity.setOrderDurationSecA(0);
        reportEntity.setOrderDurationSecB(0);
        reportEntity.setUserIdA("");
        reportEntity.setUserIdB("");
        reportEntity.setOrderIdA(0);
        reportEntity.setOrderIdB(0);
        reportEntity.setPoiid(0);
    }
}

